﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;
using TST.SharePointObjects.WebControls;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    public class FeatureDefinitionPage : SOPageBase
    {
        protected SiteObjectGrid siteObjectGrid;
        private FeatureDefinitionList _objectList;
        protected TableRow rowSiteObjectOptions;
        protected TableRow rowSiteObjectOptionsHeader;
        protected SelectedObject selectedObject;
        protected ObjectReferenceGrid referenceList;
        protected SiteInfo siteInfo;
        protected SiteObjectGrid siteObjectGridIndexed;
        protected Label labelSiteObjectOptionsHeader;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Setup SiteObject options
            rowSiteObjectOptions.Visible = false;
            rowSiteObjectOptionsHeader.Visible = false;
            referenceList.Visible = false;

            siteObjectGrid.Title = "Feature Definitions in this farm";
            siteObjectGrid.HelpText = "The list below shows all feature definitions that are installed for this SharePoint farm.";
            siteObjectGrid.HasGroups = true;
            siteObjectGrid.HasSearch = true;
            siteObjectGrid.HasHelp = false;

            siteObjectGridIndexed.Title = "Indexed Feature Definitions";
            siteObjectGridIndexed.HelpText = "The list below shows all indexed feature definitions that are installed for this SharePoint farm.";
            siteObjectGridIndexed.HasGroups = false;
            siteObjectGridIndexed.HasSearch = false;
            siteObjectGridIndexed.HasHelp = false;

            siteInfo.Database = Database;
            referenceList.Database = Database;
            selectedObject.Database = Database;

            referenceList.Title = "Object using this feature definition";

            if (Database == null)
            {
                return;
            }

            // Load items.
            _objectList = new FeatureDefinitionList(VersionInfo.License.LicensingMode, SPContext.Current.Site, SPContext.Current.Web, SPFarm.Local.Id, Database);
            _objectList.LoadFromWeb(siteObjectGrid.SelectGroup.SelectedValue, siteObjectGrid.SearchSiteObject.Text);
            siteObjectGrid.ShowIndexed = false;
            siteObjectGrid.SiteObjectList = _objectList;
            siteObjectGrid.SelectedSiteObjectChanged += new SiteObjectGrid.SelectedSiteObjectChangedHandler(siteObjectGrid_SelectedSiteObjectChanged);

            siteObjectGridIndexed.ShowIndexed = true;
            siteObjectGridIndexed.SiteObjectList = _objectList;
            siteObjectGridIndexed.SelectedSiteObjectChanged += new SiteObjectGrid.SelectedSiteObjectChangedHandler(siteObjectGrid_SelectedSiteObjectChanged);

            SetupGroups();
            GetReferenceInfo(null);

            // Events
            selectedObject.BeforeSave += new SelectedObject.BeforeSaveHandler(selectedObject_BeforeSave);
        }

        private void SetupGroups()
        {
            if (!Page.IsPostBack)
            {
                siteObjectGrid.SelectGroup.Items.Add(new ListItem("-- Select a scope --", string.Empty));
                List<string> groups = new List<string>();
                foreach (SPFeatureDefinition definition in SPFarm.Local.FeatureDefinitions)
                {
                    if (!groups.Contains(definition.Scope.ToString()))
                    {
                        groups.Add(definition.Scope.ToString());
                    }
                }
                groups.Sort();
                foreach (string group in groups)
                {
                    siteObjectGrid.SelectGroup.Items.Add(group);
                }
            }
        }

        private void selectedObject_BeforeSave(SiteObject siteObject)
        {
            if (siteObject == null)
                return;
            SPFeatureDefinition def = SPFarm.Local.FeatureDefinitions[new Guid(siteObject.Id)];
            switch (siteObject.IndexByProperty)
            {
                case IndexByProperty.InternalName:
                    siteObject.IndexId = def.Name;
                    break;
                case IndexByProperty.DisplayName:
                    siteObject.IndexId = def.Name;
                    break;
                default:
                    siteObject.IndexId = def.Id.ToString("B");
                    break;
            }
        }

        private void siteObjectGrid_SelectedSiteObjectChanged(SiteObject siteObject)
        {
            siteObject.IndexByProperty = IndexByProperty.Id;
            siteObject.IndexScope = ObjectReferenceScope.Farm;
            GetReferenceInfo(siteObject);
        }

        private void GetReferenceInfo(SiteObject selected)
        {
            selectedObject.GetReferenceInfo(selected);
            rowSiteObjectOptionsHeader.Visible = selected != null;
            rowSiteObjectOptions.Visible = selected != null;
            referenceList.Visible = selected != null;
            referenceList.GetReferenceInfo(selected, VersionInfo.License.LicensingMode);
        }

        protected override bool RequireSiteAdministrator
        {
            get
            {
                return true;
            }
        }
    }
}
